package com.boot.mgt.controller.app

import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONObject
import com.boot.config.RequestWrapper
import com.boot.core.protocol.QueryRequestOfApp
import com.boot.core.result.ResultTo
import com.boot.mgt.model.TaAccident
import com.boot.mgt.model.TaAccidentImage
import com.boot.mgt.model.TaHmsPush
import com.boot.mgt.service.TaAccidentService
import com.boot.mgt.service.TaHmsPushService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*
import java.util.*
import javax.servlet.http.HttpServletRequest

/**
 * 事故管理接口
 */
@RestController
@RequestMapping("/app/accident")
class TaAppAccidentController {

    @Autowired
    private lateinit var accidentService: TaAccidentService

    @Autowired
    private lateinit var taHmsPushService: TaHmsPushService

    /**
     * 警员同意或者拒绝
     */
    @PostMapping("/police_accept")
    fun policeAccept(has_accepted: Int, accident_report_id: Int): ResultTo {
        return accidentService.policeAccept(has_accepted, accident_report_id)
    }

    /**
     * 警员同意或者拒绝
     */
    @PostMapping("/police_accept_rpc")
    fun policeAcceptRpc(has_accepted: Int, accident_report_id: Int, police_number: String): ResultTo {
        return accidentService.policeAcceptRpc(has_accepted, accident_report_id, police_number)
    }

    /**
     * 警员转发列表
     */
    @GetMapping("/forward_list")
    fun forwardPoliceList(@RequestParam(required = false) name: String?): ResultTo {
        return accidentService.forwardPoliceList(name)
    }

    /**
     * 警员转发列表
     */
    @GetMapping("/forward_list_rpc")
    fun forwardPoliceListRpc(@RequestParam(required = false) name: String?, @RequestParam(required = false) police_number: String?, @RequestParam(required = false) org_num: String?): ResultTo {
        return accidentService.forwardPoliceListRpc(name, police_number, org_num)
    }

    /**
     * 警员转发案件给其他警员
     */
    @PostMapping("/forward_report")
    fun policeForward(police_number_session: String, police_number: String, accident_report_id: Int): ResultTo {
        return accidentService.policeForward(police_number_session, police_number, accident_report_id)
    }

    /**
     * 警员现场勘查上报
     */
    @PostMapping("/police_add_report")
    fun policeAddReport(@RequestBody taAccident: TaAccident): ResultTo {
        taAccident.state = 3
        return accidentService.policeAddReport(taAccident, false, false)
    }

    /**
     * 警员现场勘查上报
     */
    @PostMapping("/police_add_report_rpc")
    fun policeAddReportRpc(@RequestBody taAccident: TaAccident, police_number: String): ResultTo {
        taAccident.state = 3
        return accidentService.policeAddReportRpc(taAccident, false, false, police_number)
    }

    /**
     * 上传现场图片
     */
    @PostMapping("/upload_image")
    fun policeAddReportImage(@RequestBody taAccidentImage: TaAccidentImage): ResultTo {
        return accidentService.policeAddReportImage(taAccidentImage)
    }

    /**
     * 获取事故信息列表
     */
    @GetMapping("/list")
    fun list(request: HttpServletRequest,
             page_index: Int?, page_size: Int?): ResultTo {
        val map = hashMapOf<String, Any>()
        request.parameterMap?.forEach { t, u -> map[t] = u[0] }
        return accidentService.list(page_index, page_size, map)
    }

    /**
     * 获取事故信息详情
     */
    @GetMapping("/detail")
    fun detail(id: Int): ResultTo {
        return accidentService.detail(id)
    }

    /**
     * 获取事故信息详情
     */
    @GetMapping("/getMqMsg")
    fun getMqMsg(police_number: String): Any? {
        var taHmsPushList: List<TaHmsPush>? = taHmsPushService.searchMqMessage(police_number)
        if (taHmsPushList != null) {
            return ResultTo().setData("taHmsPushList", taHmsPushList)
        }
        return ResultTo()
    }

    /**
     * 获取事故信息详情
     */
    @GetMapping("/getMqMsgRpc")
    fun getMqMsgRpc(request: HttpServletRequest): Any? {
        var queryParamsObj : QueryRequestOfApp? = JSONObject.parseObject(String(Base64.getDecoder().decode((request as RequestWrapper).body)), QueryRequestOfApp::class.java)
        var map = JSON.parse(JSON.toJSONString(queryParamsObj?.params?.data)) as HashMap<String, Any>
        var police_number: String = map["police_number"] as String
        var taHmsPushList: List<TaHmsPush>? = taHmsPushService.searchMqMessage(police_number)
        return ResultTo().setData("taHmsPushList", taHmsPushList ?: arrayListOf<TaHmsPush>())
    }

}